<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2018/3/26
 * Time: 19:39
 */

/**
 * Define MyClass
 */
class MyClass {
	public $public = 'Public';
	protected $protected = 'Protected';
	private $private = 'Private';

	function printHello() {
		echo $this->public . PHP_EOL;
		echo $this->protected . PHP_EOL;
		echo $this->private . PHP_EOL;
	}
}

$obj = new MyClass();
echo $obj->public . PHP_EOL; // 这行能被正常执行
//echo $obj->protected; // 这行会产生一个致命错误
//echo $obj->private; // 这行也会产生一个致命错误
$obj->printHello(); // 输出 Public、Protected 和 Private

/**
 * Define MyClass2
 */
class MyClass2 extends MyClass {
	// 可以对 public 和 protected 进行重定义，但 private 而不能
	protected $protected = 'Protected2';

	function printHello() {
		echo $this->public . PHP_EOL;
		echo $this->protected . PHP_EOL;
		//echo $this->private;
	}
}

$obj2 = new MyClass2();
echo $obj2->public . PHP_EOL; // 这行能被正常执行
//echo $obj2->private; // 未定义 private
//echo $obj2->protected; // 这行会产生一个致命错误
$obj2->printHello(); // 输出 Public、Protected2 和 Undefined
